package com.css.fxfzfxqh.modules.buildingKilometerGrid.service;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.common.rest.JcsjRestManager;
import com.css.fxfzfxqh.constants.GeomTypeEnum;
import com.css.fxfzfxqh.constants.YNEnum;
import com.css.fxfzfxqh.exceptions.ShpFieldException;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.constant.FxqhBuildingGridTableTitleDesc;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.model.entity.FxqhBuildingGridRuralCivil;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.model.param.QueryByVersionParam;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.model.vo.BuildingGridGetByIdVo;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.model.vo.QueryByVersionVo;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.model.vo.VersionParseFileVo;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.repository.FxqhBuildingGridRuralCivilRepository;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.strategy.ShpService;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.vo.Grid13VO;
import com.css.fxfzfxqh.util.EasyPoiExcelUtil;
import com.css.fxfzfxqh.util.ParsingResult;
import com.css.fxfzfxqh.util.ShpAnalyticUtil;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 农村土木（石木）结构房屋格网汇总数据结构表
 */
@Service("nctmsmjggw")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class FxqhBuildingGridRuralCivilService implements ShpService {
    @Autowired
    FxqhBuildingGridRuralCivilRepository buildingGridRuralCivilRepository;
    ThreadLocal<Integer> shpIndex = ThreadLocal.withInitial(() -> 0); // shp文件解析下标
    @Autowired
    JcsjRestManager jcsjRestManager;

    @Override
    public VersionParseFileVo parseData(ParsingResult parsingResult, String tempVersionCode) throws ShpFieldException {
        if (parsingResult.getShpList() == null) {
            throw new ShpFieldException("文件内没有数据，请上传有数据的文件");
        }
        parseList(parsingResult.getShpList(), tempVersionCode);
//        buildingGridRuralCivilRepository.batchSave(list);
        VersionParseFileVo vo = new VersionParseFileVo();
        vo.setDataVersion(tempVersionCode);
        vo.setFieldNum(shpIndex.get());
        vo.setFieldEntry(parsingResult.getShpList().size());
        return vo;
    }

    @Override
    public void saveParseData(String sourceVersionCode, String targetVersionCode) {
        buildingGridRuralCivilRepository.changeTempToRealData(sourceVersionCode, targetVersionCode);
    }

    @Override
    public QueryByVersionVo<FxqhBuildingGridRuralCivil> queryDataByVersionCode(QueryByVersionParam param) {
        List<FxqhBuildingGridRuralCivil> list = buildingGridRuralCivilRepository.listByDataVersion(param);
        Long total = buildingGridRuralCivilRepository.countByDataVersion(param);
        return QueryByVersionVo.build(list, param, total, FxqhBuildingGridTableTitleDesc.rural_civil);
    }

    @Override
    public List<Grid13VO> queryDataByParam(QueryByVersionParam param) {
        return buildingGridRuralCivilRepository.queryDataByParam(param);
    }

    @Override
    public List<Grid13VO> queryDataByParam1(QueryByVersionParam param) {
        return buildingGridRuralCivilRepository.queryDataByParam1(param);
    }

    @Override
    public BuildingGridGetByIdVo<?> findById(String id) {
        FxqhBuildingGridRuralCivil fxqhBuildingGridRuralCivil = buildingGridRuralCivilRepository.getById(id);
        return BuildingGridGetByIdVo.build(fxqhBuildingGridRuralCivil, FxqhBuildingGridTableTitleDesc.rural_civil);
    }

    @Override
    public void delByVersionCode(String versionCode, Date currTime, String currUid) {
        buildingGridRuralCivilRepository.delByVersionCode(versionCode, currTime, currUid);
    }

    @Override
    public void delDataById(String id) {
        buildingGridRuralCivilRepository.delDataById(id);
    }

    private void parseList(List<List<Object>> shpList, String dataVersion) throws ShpFieldException {
        if (CollectionUtils.isEmpty(shpList)) {
            return;
        }
//        List<FxqhBuildingGridRuralCivil> fxqhBuildingGridRuralCivils = new ArrayList<>();
        int index = 0;
        for (List<Object> list : shpList) {
            log.debug(list.toString());
            index = 0;
            FxqhBuildingGridRuralCivil fxqhBuildingGridRuralCivil = new FxqhBuildingGridRuralCivil();
            fxqhBuildingGridRuralCivil.setDataVersion(dataVersion);
            fxqhBuildingGridRuralCivil.setCreateUser(PlatformSessionContext.getUserID());
            fxqhBuildingGridRuralCivil.setCreateTime(new Date());
            fxqhBuildingGridRuralCivil.setDelFlag(YNEnum.N.toString());
            fxqhBuildingGridRuralCivil.setId(IdUtil.getSnowflakeNextIdStr());
            String geom = ShpAnalyticUtil.checkGeomTypeOf(list.get(index++), GeomTypeEnum.MULTIPOLYGON);
            if (StringUtils.isBlank(geom)) {
                continue;
            }
            fxqhBuildingGridRuralCivil.setGeom(geom);
            fxqhBuildingGridRuralCivil.setObjectid(ShpAnalyticUtil.getDoubleVal(list.get(index++), "OBJECTID", true));
            fxqhBuildingGridRuralCivil.setOid1(ShpAnalyticUtil.getDoubleVal(list.get(index++), "OID_1", true));
            fxqhBuildingGridRuralCivil.setGridRows(ShpAnalyticUtil.getDoubleVal(list.get(index++), "ROWS", true));
            fxqhBuildingGridRuralCivil.setColums(ShpAnalyticUtil.getDoubleVal(list.get(index++), "COLUMS", true));
            fxqhBuildingGridRuralCivil.setAreaCode(ShpAnalyticUtil.getStringVal(list.get(index++), "AREA_CODE", true));
            fxqhBuildingGridRuralCivil.setShapeLeng(ShpAnalyticUtil.getDoubleVal(list.get(index++), "SHAPE_LENG", true));
            fxqhBuildingGridRuralCivil.setShapeLe1(ShpAnalyticUtil.getDoubleVal(list.get(index++), "SHAPE_LE_1", true));
            fxqhBuildingGridRuralCivil.setShapeArea(ShpAnalyticUtil.getDoubleVal(list.get(index++), "SHAPE_AREA", true));
            fxqhBuildingGridRuralCivil.setTotalArea(ShpAnalyticUtil.getDoubleVal(list.get(index++), "总面积", true));
            fxqhBuildingGridRuralCivil.setHousecount(ShpAnalyticUtil.getIntegerVal(list.get(index++), "栋数", true));
            fxqhBuildingGridRuralCivil.setResidence(ShpAnalyticUtil.getDoubleVal(list.get(index++), "住宅房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setOffice(ShpAnalyticUtil.getDoubleVal(list.get(index++), "办公房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setBusiness(ShpAnalyticUtil.getDoubleVal(list.get(index++), "商业房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setMedic(ShpAnalyticUtil.getDoubleVal(list.get(index++), "医疗教育面积占比", true));
            fxqhBuildingGridRuralCivil.setOther(ShpAnalyticUtil.getDoubleVal(list.get(index++), "其他房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setY1980(ShpAnalyticUtil.getDoubleVal(list.get(index++), "1980年及以前房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setY1981Y1990(ShpAnalyticUtil.getDoubleVal(list.get(index++), "1981_1990年房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setY1991Y2000(ShpAnalyticUtil.getDoubleVal(list.get(index++), "1991-2000年房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setY2001Y2010(ShpAnalyticUtil.getDoubleVal(list.get(index++), "2000-2010年房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setY2011Y2015(ShpAnalyticUtil.getDoubleVal(list.get(index++), "2011-2015年房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setY2016(ShpAnalyticUtil.getDoubleVal(list.get(index++), "2016及以后年房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setF1(ShpAnalyticUtil.getDoubleVal(list.get(index++), "1层房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setF2F3(ShpAnalyticUtil.getDoubleVal(list.get(index++), "2-3层房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setF4F6(ShpAnalyticUtil.getDoubleVal(list.get(index++), "4-6层房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setF7F10(ShpAnalyticUtil.getDoubleVal(list.get(index++), "7-10层房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setF11F16(ShpAnalyticUtil.getDoubleVal(list.get(index++), "11-16层房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setF17F30(ShpAnalyticUtil.getDoubleVal(list.get(index++), "17-30层房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setF31(ShpAnalyticUtil.getDoubleVal(list.get(index++), "31层及以上房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setKzsf0(ShpAnalyticUtil.getDoubleVal(list.get(index++), "农村房屋不设防房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setKzsf1(ShpAnalyticUtil.getDoubleVal(list.get(index++), "农村房屋6度设防房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setKzsf2(ShpAnalyticUtil.getDoubleVal(list.get(index++), "农村房屋7度设防房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setKzsf3(ShpAnalyticUtil.getDoubleVal(list.get(index++), "农村房屋8度设防房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setKzsf4(ShpAnalyticUtil.getDoubleVal(list.get(index++), "农村房屋9度房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setProfes(ShpAnalyticUtil.getDoubleVal(list.get(index++), "专业设计房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setReinforce(ShpAnalyticUtil.getDoubleVal(list.get(index++), "加固房屋面积占比", true));
            fxqhBuildingGridRuralCivil.setProvince(ShpAnalyticUtil.getStringVal(list.get(index++), "省名称", false));
            fxqhBuildingGridRuralCivil.setProvinceCode(ShpAnalyticUtil.getStringVal(list.get(index++), "省CODE", false));
            fxqhBuildingGridRuralCivil.setCity(ShpAnalyticUtil.getStringVal(list.get(index++), "市名称", false));
            fxqhBuildingGridRuralCivil.setCityCode(ShpAnalyticUtil.getStringVal(list.get(index++), "市CODE", false));
            fxqhBuildingGridRuralCivil.setCounty(ShpAnalyticUtil.getStringVal(list.get(index++), "县名称", false));
            fxqhBuildingGridRuralCivil.setCountyCode(ShpAnalyticUtil.getStringVal(list.get(index++), "县CODE", false));
            fxqhBuildingGridRuralCivil.setKilometerGridId(ShpAnalyticUtil.getStringVal(list.get(index), "格网唯一ID", true));
            fxqhBuildingGridRuralCivil.setExtends1(ShpAnalyticUtil.getStringVal(list.get(index), "格网唯一ID", true));
            buildingGridRuralCivilRepository.insert(fxqhBuildingGridRuralCivil);
//            try {
//                GetChinaGridBo chinaGridIdNew = jcsjRestManager.getChinaGridIdNew(fxqhBuildingGridRuralCivil.getGeom(), fxqhBuildingGridRuralCivil.getProvince(), fxqhBuildingGridRuralCivil.getCity(), fxqhBuildingGridRuralCivil.getCounty());
//                if (chinaGridIdNew.getCode() == 200) {
//                    fxqhBuildingGridRuralCivil.setExtends1(chinaGridIdNew.getData());
//                }
//            } catch (Exception e) {
//                log.error(e.getMessage(), e);
//            }
//            fxqhBuildingGridRuralCivils.add(fxqhBuildingGridRuralCivil);
        }
        shpIndex.set(index);
//        return fxqhBuildingGridRuralCivils;
    }

    @Override
    public List<FxqhBuildingGridRuralCivil> listGridDataByVersionCode(String version) {
        return buildingGridRuralCivilRepository.listGridDataByVersionCode(version);
    }

    @Override
    public void dataExport(HttpServletResponse response, String versionCode) {
        List<FxqhBuildingGridRuralCivil> list = buildingGridRuralCivilRepository.listByDataVersion(QueryByVersionParam.builder().versionCode(versionCode).build());
        EasyPoiExcelUtil.exportExcel(list, "农村土木（石木）结构房屋格网汇总数据结构表", "sheet1", FxqhBuildingGridRuralCivil.class, "农村土木（石木）结构房屋格网汇总数据结构表", response);
    }

    @Override
    public List<Grid13VO> queryYwData(QueryByVersionParam queryByVersionParam) {
        return buildingGridRuralCivilRepository.queryYwData(queryByVersionParam);
    }

    @Override
    public List<Grid13VO> getJcld(QueryByVersionParam queryByVersionParam) {
        return buildingGridRuralCivilRepository.getJcld(queryByVersionParam);
    }

    @Override
    public List<Grid13VO> getPga(QueryByVersionParam queryByVersionParam) {
        return buildingGridRuralCivilRepository.getPga(queryByVersionParam);
    }
}